package com.alibaba.csp.sentinel.dashboard.rule.apollo;

import java.util.Collections;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.alibaba.csp.sentinel.dashboard.datasource.entity.rule.FlowRuleEntity;
import com.alibaba.csp.sentinel.dashboard.rule.DynamicRuleProvider;
import com.alibaba.csp.sentinel.datasource.Converter;
import com.ctrip.framework.apollo.openapi.client.ApolloOpenApiClient;
import com.ctrip.framework.apollo.openapi.dto.OpenItemDTO;
import com.ctrip.framework.apollo.openapi.dto.OpenNamespaceDTO;

@Component("flowRuleApolloProvider")
public class FlowRuleApolloProvider implements DynamicRuleProvider<List<FlowRuleEntity>> {
	
	@Autowired
	private Converter<String, List<FlowRuleEntity>> converter;
	
	@Override
	public List<FlowRuleEntity> getRules(String appName) throws Exception {
		
		ApolloOpenApiClient client = ApolloConfigUtil.createApolloOpenApiClient(appName);
		
		if(client != null) {
			//	具体的流控规则id
			String flowDataId = ApolloConfigUtil.getFlowDataId(appName);
			//	apollo的应用服务appId
			String appId = ApolloConfigUtil.getAppIdWithAppName(appName);
			
			OpenNamespaceDTO dto = client.getNamespace(appId, ApolloConfig.ENV, ApolloConfig.CLUSTERNAME, ApolloConfig.NAMESPACE);
			
			String rules = dto
			.getItems()
			.stream()
			.filter(p -> p.getKey().equals(flowDataId))
			.map(OpenItemDTO::getValue)
			.findFirst().orElse("");
			
			return converter.convert(rules);
			
		} else {
			return Collections.emptyList();
		}
		
	}

}
